package control;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.letter.ListVO;
import model.member.MemberVO;
import model.qna.QnABoardService;
import model.qna.QnABoardVO;
import model.qna.QnAListVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class QnAController extends MultiActionController {
	private QnABoardService qnaService;

	public QnAController(QnABoardService service) {
		super();
		this.qnaService = service;
	}


	/*public ModelAndView qnaListView(HttpServletRequest request,HttpServletResponse response, QnABoardVO bvo) throws SQLException{
		
			qna hr목록 보기 
		 
		return new ModelAndView("list.qna");
		
	}*/
	
	public ModelAndView qnaWriteView(HttpServletRequest request,HttpServletResponse response, QnABoardVO bvo){//qna_write.jsp 로 가는곳
		return new ModelAndView("write.qna");
	}
	
	public ModelAndView qnaWrite(HttpServletRequest request,HttpServletResponse response, QnABoardVO qbvo) throws SQLException{
		//글쓰기method
		HttpSession session = request.getSession();
		MemberVO mvo=(MemberVO) session.getAttribute("mvo");
		qbvo.setMember(mvo);
		System.out.println(qbvo);
		try {
			qnaService.qnaWrite(qbvo);
			System.out.println(qbvo);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return new ModelAndView("getcontent.qna","qnabvo",qbvo);
	}
	
		
	public ModelAndView qnaCheckPasswordView(HttpServletRequest request,HttpServletResponse response){
		// 비밀번호 체크 패스워드 뷰~!
		request.setAttribute("page", request.getParameter("page"));
		return new ModelAndView("qna/qna_check_password");
	}
	
	public ModelAndView qnaGetContent(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		//글 읽기 method
		int qnaNo=Integer.parseInt(request.getParameter("qnaNo"));
		QnABoardVO qvo =qnaService.qnaGetContent(qnaNo);
		request.setAttribute("page", request.getParameter("page"));
		System.out.println(qvo.getMember());
		return new ModelAndView("getcontent.qna","qnabvo",qvo);
	}
	
	public ModelAndView qnaUpdateGetContent(HttpServletRequest request,HttpServletResponse response, QnABoardVO bvo) throws SQLException{
		//업데이트전 글 불러오기 method
		System.out.println("글 번호 불러오기 " + bvo.getQnaNo());
		QnABoardVO qnavVo =  qnaService.qnaUpdateGetContent(bvo.getQnaNo());
		
		return new ModelAndView("update.qna","qnavo",qnavVo);
	}
	
	public ModelAndView qnaDelete(HttpServletRequest request,HttpServletResponse response,QnABoardVO bvo) throws SQLException{
		//글삭제  method
		HttpSession session = request.getSession();
		qnaService.qnaDelete(bvo.getQnaNo());
		System.out.println("삭제완료");
		return new ModelAndView("delete_result.qna");
	}
	
	public ModelAndView qnaReWriteBeforContent(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		//답글 쓰기 전 글 불러오기 method
		int qnaNo = Integer.parseInt(request.getParameter("qnaNo"));
		QnABoardVO qnABoardVO = qnaService.qnaUpdateGetContent(qnaNo);
		request.setAttribute("page", request.getParameter("page"));
		return new ModelAndView("re_write.qna","qnABoardVO",qnABoardVO);
	}
	
	public ModelAndView qnaReWrite(HttpServletRequest request,HttpServletResponse response,QnABoardVO vo) throws SQLException{
		//답글 쓰기 method
		MemberVO member = (MemberVO) request.getSession().getAttribute("mvo");
		vo.setMember(member);
		System.out.println(vo);
		qnaService.qnaReWrite(vo);
		request.setAttribute("page", request.getParameter("page"));
		return new ModelAndView("getcontent.qna","qnabvo",vo);
	}
	
	public ModelAndView qnaAllBoardList(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		//전체 게시불 불러오기 
		String pageNo = request.getParameter("page");
		QnAListVO listVO  = qnaService.qnaListView(pageNo);
		System.out.println(listVO);
		
		return new ModelAndView("boardList.qna","listVO",listVO);
	}
	
	public ModelAndView qnaUpdate(HttpServletRequest request,HttpServletResponse response,QnABoardVO qnaVO) throws SQLException{
	// 글 업데이트 게시물
	MemberVO mvo = (MemberVO) request.getSession().getAttribute("mvo");
	System.out.println(mvo);
	qnaVO.setMember(mvo);
	System.out.println("결과 : " + qnaVO);
	qnaService.qnaUpdate(qnaVO);
	QnABoardVO vo= qnaService.qnaUpdateGetContent(qnaVO.getQnaNo());
		return new ModelAndView("getcontent.qna","qnabvo",vo);
	}
	
	public ModelAndView qnaCheckPassword(HttpServletRequest request,HttpServletResponse response,QnABoardVO qnaVO) throws SQLException{
		//글 읽기전 패스워드 확인 method
		System.out.println("체크 결과" +qnaVO);
		boolean flag = qnaService.qnaCheckPassword(qnaVO);
		return new ModelAndView("JsonView","flag",flag);
	}
	
	public ModelAndView qnaGetMaxQnaRestep (HttpServletRequest request,HttpServletResponse response,QnABoardVO qnaVO) throws SQLException{
		//답글이 달려있는지 않달려있는지 확인하는 method
		System.out.println("QnaRef :"+qnaVO.getQnaRef());
		int qnaRestep =  qnaService.qnaGetMaxQnaRestep(qnaVO.getQnaRef());
		System.out.println(qnaRestep);
		return new ModelAndView("JsonView","step",qnaRestep);
	}

}
